//------------------------------------------------------------------------
// <copyright file="MainController.cs" company="DevDir">
//   Copyleft (c) DevDir. No rights reserved.
// </copyright>
// <author>Nikita Sharov</author>
//------------------------------------------------------------------------

namespace DevDir.Mp3.Equilibrist
{
  using System;
  using System.Collections.Generic;
  using System.Diagnostics;
  using System.Windows.Forms;
  using DevDir.Mp3.Equilibrist.Forms;
  
  /// <summary>
  /// Handles user actions on the <see cref="MainForm"/>.
  /// </summary>
  public class MainController
  {
    private MainForm mainForm;
    private AboutForm aboutForm = new AboutForm();
    private OptionsForm optionsForm = new OptionsForm();
    private Dictionary<ToolStripButton, IModule> modules = 
      new Dictionary<ToolStripButton, IModule>();
    private IModule currentModule;

    /// <summary>
    /// Initializes a new instance of the <b>MainController</b> class.
    /// </summary>
    public MainController()
    {
      
    }

    /// <summary>
    /// Gets or sets the controlled view.
    /// </summary>
    public MainForm MainForm
    {
      get 
      { 
        return this.mainForm; 
      }

      set
      {
        if (this.mainForm != value)
        {
          this.mainForm = value;
          this.OnMainFormChanged();
        }
      }
    }

    /// <summary>
    /// Processes view change.
    /// </summary>
    protected virtual void OnMainFormChanged()
    {
      foreach (ToolStripMenuItem item in this.mainForm.MenuItems)
      {
        item.Click += new EventHandler(this.MenuItemClick);
      }

      this.currentModule = new TaggerController();      
      
      this.modules.Add(
        this.mainForm.TaggerButton, this.currentModule);
      this.modules.Add(
        this.mainForm.LookupButton, new LookupController());
      foreach (ToolStripButton button in this.modules.Keys)
      {
        button.Click += new EventHandler(this.ToolStripButtonClick);
      }

      this.currentModule.Enter(this.mainForm);

      FileExplorer.Instance.TreeView = this.mainForm.TreeView;
      FileExplorer.Instance.Refresh();
    }

    private void MenuItemClick(object sender, System.EventArgs e)
    {
      ToolStripMenuItem item = (ToolStripMenuItem)sender;
      switch (item.Name)
      {
        case "mnuFileExit":
          this.mainForm.Close();
          break;
        case "mnuViewExplorer":
          break;
        case "mnuViewTagger":
          break;
        case "mnuViewLookup":
          break;
        case "mnuToolsOptions":
          this.optionsForm.ShowDialog();
          break;
        case "mnuHelpAbout":
          this.aboutForm.ShowDialog();
          break;
        default:
          // nthing
          break;
      }
    }

    private void ToolStripButtonClick(object sender, EventArgs e)
    {
      ToolStripButton item = (ToolStripButton)sender;
      IModule module = this.modules[item];
      if (module != this.currentModule)
      {
        if (this.currentModule != null)
        {
          this.currentModule.Leave(this.mainForm);
        }

        this.currentModule = module;
        this.currentModule.Enter(this.mainForm);
      }
    }
  }
}
